package com.bootdo.program.wallpaper.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.program.wallpaper.domain.AppConnectDO;
import com.bootdo.program.wallpaper.domain.BzConfigDO;
import com.bootdo.program.wallpaper.domain.BzTalentDO;
import com.bootdo.program.wallpaper.service.AppConnectService;
import com.bootdo.program.wallpaper.service.BzConfigService;
import com.bootdo.program.wallpaper.service.BzTalentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.program.wallpaper.domain.SettlemendaysBillDO;
import com.bootdo.program.wallpaper.service.SettlemendaysBillService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 用户每日结算账单
 * 
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-11-09 21:08:21
 */
 
@Controller
@RequestMapping("/wallpaper/settlemendaysBill")
public class SettlemendaysBillController extends BaseController {
	@Autowired
	private SettlemendaysBillService settlemendaysBillService;

	@Autowired
	private BzConfigService bzConfigService;

	@Autowired
	private BzTalentService bzTalentService;


	@GetMapping()
	@RequiresPermissions("wallpaper:settlemendaysBill:settlemendaysBill")
	String SettlemendaysBill(Model model){
		List<BzConfigDO> platform_type = bzConfigService.getType("platform_type");
		model.addAttribute("platform_type",platform_type);
	    return "wallpaper/settlemendaysBill/settlemendaysBill";
	}


	@ResponseBody
	@GetMapping("/getCount")
	public R getCount(){
		Map<String, Object> params = new HashMap<>();
		params.put("status",1);
		//params.put("appid",queryAppid());

		int count1 = settlemendaysBillService.count(params);
		params.put("status",2);
		int count2 = settlemendaysBillService.count(params);
		params.put("status",3);
		int count3 = settlemendaysBillService.count(params);

		return  R.ok().put("count1","待结算("+count1+")").put("count2","已结算("+count2+")").put("count3","已打款("+count3+")");
	}


	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wallpaper:settlemendaysBill:settlemendaysBill")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SettlemendaysBillDO> settlemendaysBillList = settlemendaysBillService.list(query);
		settlemendaysBillList.forEach((SettlemendaysBillDO) -> {
			BzTalentDO talentDO = bzTalentService.getByUserId(SettlemendaysBillDO.getUserid());
			SettlemendaysBillDO.setUserName(null != talentDO ? talentDO.getNickname() : null);
		});
		int total = settlemendaysBillService.count(query);
		PageUtils pageUtils = new PageUtils(settlemendaysBillList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wallpaper:settlemendaysBill:add")
	String add(Model model){
		List<BzConfigDO> platform_type = bzConfigService.getType("platform_type");
		model.addAttribute("platform_type",platform_type);
	    return "wallpaper/settlemendaysBill/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wallpaper:settlemendaysBill:edit")
	String edit(@PathVariable("id") Long id,Model model){
		SettlemendaysBillDO settlemendaysBill = settlemendaysBillService.get(id);
		model.addAttribute("settlemendaysBill", settlemendaysBill);

		List<BzConfigDO> platform_type = bzConfigService.getType("platform_type");
		model.addAttribute("platform_type",platform_type);
	    return "wallpaper/settlemendaysBill/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@GetMapping("/save")
	@RequiresPermissions("wallpaper:settlemendaysBill:add")
	public R save(BigDecimal ecpmMoney,String sTime,Integer settlemantPlatform) throws Exception {

		if(null == sTime && "".equals(sTime)){
			return R.error("请选择结算日期");
		}
		if(null == ecpmMoney){
			return R.error("请输入ecpm金额");
		}
		if(null == settlemantPlatform){
			return R.error("请选择结算平台");
		}

		if(settlemendaysBillService.countSett(sTime,settlemantPlatform) > 0){
			return R.error("系统错误，当日已生成结算单");
		}

		int count = settlemendaysBillService.generateSettlement(ecpmMoney, getUserId(),sTime,settlemantPlatform);

		if(count == -1){
			return R.error("没有要结算的数据");
		}

		if(count == 0){
			return R.error("结算失败");
		}

		if(count == -2){
			return R.error("请先配置小程序基础信息");
		}

		return R.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wallpaper:settlemendaysBill:edit")
	public R update( SettlemendaysBillDO settlemendaysBill){
		settlemendaysBillService.update(settlemendaysBill);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wallpaper:settlemendaysBill:remove")
	public R remove( Long id){
		if(settlemendaysBillService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wallpaper:settlemendaysBill:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		settlemendaysBillService.batchRemove(ids);
		return R.ok();
	}

	@GetMapping("/addWallet")
	@RequiresPermissions("wallpaper:settlemendaysBill:add")
	String addWallet(Model model){
		List<BzConfigDO> platform_type = bzConfigService.getType("platform_type");
		model.addAttribute("platform_type",platform_type);
		return "wallpaper/settlemendaysBill/addWallet";
	}

	/**
	 * 钱包入账，批量，按照日期入账
	 */
	@ResponseBody
	@PostMapping("/addWalletInfo")
	@RequiresPermissions("wallpaper:settlemendaysBill:add")
	public R addWalletInfo(String sTime,Integer settlemantPlatform) throws Exception {

		String message = getMessage();
		if(null != message){
			return R.error(message);
		}

		if(StringUtils.isEmpty(sTime)){
			return R.error("请选择入账日期");
		}

		if(null == settlemantPlatform){
			return R.error("请选择入账平台");
		}

		String time = sTime + " 00:00:00";

		int i = settlemendaysBillService.addWalletInfo(time,settlemantPlatform);
		if(i == -1){
			return R.error("没有可入账的信息");
		}
		if(i > 0){
			return R.ok("入账成功");
		}

		return R.error("入账失败");
	}

}
